<?php
/**
 * Leek - Zend Framework Components
 *
 * @category   Leek
 * @package    Leek_Log
 * @author     Chris Jones <leeked@gmail.com>
 * @link       http://code.google.com/p/leek-zf-components/
 * @license    http://www.opensource.org/licenses/bsd-license.php New BSD License
 * @version    $Id: Config.php 74 2009-10-25 17:49:15Z leeked $
 */

/**
 * Log helper class
 *
 * @category   Leek
 * @package    Leek_Log
 * @author     Chris Jones <leeked@gmail.com>
 * @link       http://code.google.com/p/leek-zf-components/
 * @license    http://www.opensource.org/licenses/bsd-license.php New BSD License
 */
class Leek_Log
{
    const REGISTRY_PREFIX = 'Zend_Log-';

    /**
     * Get a log out of the registry.
     *
     * @param string $key
     * @return Zend_Log
     * @static
     */
    static public function getRegistryLog($key)
    {
        if (!Zend_Registry::isRegistered(self::REGISTRY_PREFIX . $key)) {
            return false;
        }

        return Zend_Registry::get(self::REGISTRY_PREFIX . $key);
    }

    /**
     * Stores a log in the registry.
     *
     * @param string $key
     * @param Zend_Log $logger
     * @return Zend_Log
     * @static
     */
    static public function setRegistryLog($key, $logger)
    {
        return Zend_Registry::set(self::REGISTRY_PREFIX . $key, $logger);
    }

    /**
     * Get a log out of the log bootstrap.
     *
     * @param string $key
     * @return Zend_Log
     * @static
     */
    static public function getBootstrapLog($key)
    {
        if ($logger = self::getRegistryLog($key)) {
            return $logger;
        }

        $log = Zend_Controller_Front::getInstance()->getParam('bootstrap')->getResource('log');
        $log = isset($log[$key]) ? $log[$key] : false;

        if ($log) {

            $writer = 'Zend_Log_Writer_' . $log['writer'];
            $writer = new $writer($log['path']);
            $logger = new Zend_Log($writer);

            self::setRegistryLog($key, $logger);
            return $logger;
        }

        return false;
    }
}
